package com.ec.core.dao;

import java.sql.Connection;
import java.sql.SQLException;
import com.ec.core.util.Logging;
import com.ec.core.manage.AppFactory;

public class Transaction {
	private Connection conn = null;
	
	public Transaction(){
		try{
			conn = AppFactory.getAppFactory().getConnectionManage().getConnection();

		}catch(SQLException sqle){
			Logging.getLog("dao").error("get connection error",sqle);
		}
		if(conn!=null){
			try{
				conn.setAutoCommit(false);
			}catch(SQLException sqle){
				Logging.getLog("dao").error("set auto commit true error",sqle);
			}
		}
	}
	
	public void commit(){
		if(conn!=null){
			try{
				conn.commit();
			}catch(SQLException sqle){
				Logging.getLog("dao").error("commit transaction error",sqle);
			}
			try{
				conn.setAutoCommit(true);
			}catch(SQLException sqle){
				Logging.getLog("dao").error("set auto commit true error",sqle);
			}
		}
	}
	public void rollback(){
		if(conn!=null){
			try{
				conn.rollback();
			}catch(SQLException sqle){
				Logging.getLog("dao").error("rollback transaction error",sqle);
			}
			try{
				conn.setAutoCommit(true);
			}catch(SQLException sqle){
				Logging.getLog("dao").error("set auto commit true error",sqle);
			}
		}
	}
	public void close(){
		if(conn!=null){
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
				Logging.getLog("dao").error("close connection error",e);
			}
		}
	}
	
	public Connection getConnection(){
		return conn;
	}
	
}
